/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.continew.admin.hanfu.core.oss.OSS;
import top.continew.admin.hanfu.domain.OssFile;
import top.continew.admin.hanfu.domain.PlainUser;
import top.continew.admin.hanfu.domain.bo.PlainUserBo;
import top.continew.admin.hanfu.domain.dto.PlainUserDto;
import top.continew.admin.hanfu.domain.query.PlainUserQuery;
import top.continew.admin.hanfu.domain.vo.PlainUserVo;
import top.continew.admin.hanfu.mapper.PlainUserMapper;
import top.continew.admin.hanfu.service.IPlainUserService;
import java.util.List;

/**
 * @author xieling
 * @version 1.0
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class PlainUserServiceImpl extends ServiceImpl<PlainUserMapper, PlainUser> implements IPlainUserService {
    private final OSS oss;
    private final PlainUserMapper plainUserMapper;

    @Override
    public int addUser(PlainUserBo plainUserBo) {
        PlainUser plainUser = new PlainUser();

        List<OssFile> userImg = oss.saveFile("user_img", plainUserBo.getUploader() + "", plainUserBo.getFile());

        plainUser.setUserInfo(plainUserBo.getUserInfo());
        plainUser.setUserJob(plainUserBo.getUserJob());
        plainUser.setUserImg(userImg.get(0).getOssId() + "");
        plainUser.setUserName(plainUserBo.getUserName());
        plainUser.setStatus(plainUserBo.getStatus());
        plainUser.setUploader(plainUserBo.getUploader());
        plainUser.setUpdateTime(String.valueOf(System.currentTimeMillis()));

        boolean save = save(plainUser);

        return save ? 1 : 0;
    }

    /**
     * 查询
     */
    @Override
    public Page<PlainUserVo> getUserListByCondition(PlainUserQuery plainUserQuery) {

        PlainUserBo plainUserBo = new PlainUserBo();

        plainUserBo.setUserName(plainUserQuery.getUserName());
        plainUserBo.setUserInfo(plainUserQuery.getUserInfo());
        plainUserBo.setUploader(plainUserQuery.getUploader());
        plainUserBo.setStatus(plainUserQuery.getStatus());
        plainUserBo.setUserJob(plainUserQuery.getUserJob());

        return plainUserMapper.getUserListByCondition(new Page<>(plainUserQuery.getPageNum(), plainUserQuery
            .getPageSize()), plainUserBo);
    }

    @Override
    public int updateUser(PlainUserDto plainUserDto) {

        boolean update = lambdaUpdate().eq(PlainUser::getId, plainUserDto.getId())
            .set(plainUserDto.getUserInfo() != null, PlainUser::getUserInfo, plainUserDto.getUserInfo())
            .set(plainUserDto.getUserJob() != null, PlainUser::getUserJob, plainUserDto.getUserJob())
            .set(plainUserDto.getStatus() != null, PlainUser::getStatus, plainUserDto.getStatus())
            .set(plainUserDto.getUserName() != null, PlainUser::getUserName, plainUserDto.getUserName())
            .set(plainUserDto.getUploader() != null, PlainUser::getUploader, plainUserDto.getUploader())
            .set(PlainUser::getUpdateTime, System.currentTimeMillis())
            .update();

        return update ? 1 : 0;
    }

    @Override
    public int updateImg(PlainUserBo plainUserBo) {

        List<OssFile> userImg = oss.saveFile("user_img", plainUserBo.getUploader() + "", plainUserBo.getFile());

        boolean update = lambdaUpdate().eq(PlainUser::getId, plainUserBo.getId())
            .set(PlainUser::getUserImg, userImg.get(0).getOssId())
            .update();

        return update ? 1 : 0;
    }

}
